<?php
// This needs to be completed, but removing it now.
// inbox data
// Get  number of rows
$sql = "SELECT count(id) FROM ".$this->db."user_messages WHERE recipient=".$_SESSION['internalKey']."";
$rs=$this->dbQuery($sql);
$countrows = $this->fetchRow($rs);
$inbox_num_rows_total = $countrows['count(id)'];
if( !isset( $_REQUEST['int_cur_position'] ) || $_REQUEST['int_cur_position'] == 0 ){
	$int_cur_position = 0;
} else {
	$int_cur_position = $_REQUEST['int_cur_position'];
}
// Number of result to display on the page, will be in the LIMIT of the sql query also
$int_num_result = 3; //$this->config['number_of_messages'];
$extargv = ""; // extra argv here (could be anything depending on your page)
include_once("includes/paginate.inc.php");
// New instance of the Paging class, you can modify the color and the width of the html table
$p = new Paging( $inbox_num_rows_total, $int_cur_position, $int_num_result, $extargv );
// Load up the 2 array in order to display result
$array_paging = $p->getPagingArray();
$array_row_paging = $p->getPagingRowArray();
// change to new bootstrap pager style
// Display the result as you like...
$pager = "<li>";
$pager .= $this->_lang['showing']." ". $array_paging['lower'];
$pager .=  " ".$this->_lang['to']." ". $array_paging['upper'];
$pager .=  " (". $array_paging['total']." ".$this->_lang['total'].")";
$pager .=  "</li><li>". $array_paging['previous_link'] ."<<</a></li>" ;
for( $i=0; $i<sizeof($array_row_paging); $i++ ){
$pager .=  "<li>".$array_row_paging[$i] ."</li>";
}
$pager .=  "<li>".$array_paging['next_link'] .">></a></li>";

$sql = "SELECT * FROM ".$this->db."user_messages WHERE ".$this->db."user_messages.recipient=".$_SESSION['internalKey']." ORDER BY postdate DESC LIMIT ".$int_cur_position.", ".$int_num_result;
$inbox_rs = $this->dbQuery($sql);
$inbox_limit = $this->recordCount($inbox_rs);


?>
<section class="content-header">
    <h1>
        <?php echo $this->_lang['messages_title']; ?>
    </h1>
    <ol class="breadcrumb">
        <li><a href="./"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        <li class="active"><i class="fa fa-envelope"></i> <?php echo $this->_lang['messages_title']; ?></li>
    </ol>
</section>

<!-- Main content -->
<section class="content">
	<div class="box box-solid">
    	<div class="box-body">
        	<div class="row">
            	<div class="col-md-3 col-sm-4">
                	<div class="box-header"><i class="fa fa-inbox"></i> <h3 class="box-title" id="messages-folder-title">INBOX</h3></div>
                    <a data-target="#compose-modal" data-toggle="modal" class="btn btn-block btn-primary"><i class="fa fa-pencil"></i> Compose Message</a>
                    <ul class="nav nav-tabs tabs-left">
                        <li class="active"><a data-toggle="tab" href="#messages-inbox"><i class="fa fa-inbox"></i> Inbox (<?php echo $inbox_num_rows_total;?>)</a></li>
                        <li><a data-toggle="tab" href="#messages-sent"><i class="fa fa-mail-forward"></i> Sent</a></li>
                    </ul>
                </div>
                <div class="col-md-9 col-sm-8">
                	<div class="tab-content">
                    	<div class="tab-pane active" id="messages-inbox">
                        	<div class="table-responsive">
                            	<table class="table table-mailbox">
                                	<thead>
                                        <tr>
                                        	<th></th>
                                            <th><?php echo $this->_lang['messages_subject']; ?></th>
                  							<th><?php echo $this->_lang['messages_from']; ?></th>
                  							<th><?php echo $this->_lang['messages_private']; ?></th>
              								<th><?php echo $this->_lang['messages_sent']; ?></td>
                                        </tr>
                                    </thead>
                                    <tbody>
                                    	<?php
											if ($inbox_num_rows_total > 0 ) {
												for ($i = 0; $i < $inbox_limit; $i++) {
													$message = $this->fetchRow($inbox_rs);
													$sender = $message['sender'];
													if($sender==0) {
														$sendername = "[System]";
													} else {
														$sql = "SELECT username FROM ".$this->db."manager_users WHERE id=$sender";
														$rs2 = $this->dbQuery($sql);
														$row2 = $this->fetchRow($rs2);
														$sendername = $row2['username'];
													}
													$messagestyle = $message['messageread']==0 ? "unread" : "";
													$messagebullet = $message['messageread']==0 ? "fa-circle" : "fa-circle-o";
													?>
                                        <tr class="<?php $messagestyle;?>">
                                        	<td class="small-col"><i class="fa <?php echo $messagebullet;?>"></i></td>
                                        	<td class="subject"><a href="javascript:;" onclick="Etomite.myMessages('id=<?php echo $message['id']; ?>&m=r');"><?php echo $message['subject']; ?></a></td>
                                            <td class="name"><?php echo $sendername;?></td>
                                            <td><?php echo $message['private']==0 ? "" : "<i class='fa fa-check'></i>"; ?></td>
                                            <td class="time"><?php echo strftime($this->config['date_format'].' @ '.$this->config['time_format'], $message['postdate']+$this->config['server_offset_time']); ?></td>
                                        </tr>
                                                    <?php
												}// end for loop
											} else {
												?>
                                        <tr>
                                        	<td colspan="4"><?php echo $this->_lang['messages_no_messages'];?></td>
                                        </tr>
                                                <?php	
											}
										?>
                                    </tbody>
                                </table>
                            </div><!-- end table responsive -->
                            <div class="pager-box">
                            	<ul class="pagination pagination-sm no-margin pull-right">
								<?php echo $pager;?>
                                </ul>
                            </div>
                        </div><!-- end messages-inbox -->
                        <div class="tab-pane" id="messages-sent"><p>this is the sent area</p></div>
                    </div>
                </div>
            </div>
        </div><!-- end box body -->
    </div><!-- end box -->
</section>